package com.qk.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl  implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<Course> list(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
//1.设置PageHeLper分页参数
        PageHelper.startPage(page, pageSize);

        //2.调用mapper查询
        List<Course> courseList = courseMapper.selectlist(name, subject, target);

        //3.封装成pageResult并返回
        Page<Course> p = (Page<Course>) courseList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void add(Course course) {
        //补全属性
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.add(course);

    }

    @Override
    public Course selectById(Integer id) {
        return courseMapper.selectById(id);

    }

    @Override
    public void update(Course course) {
        //补全属性
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);

    }

    @Override
    public List<Course> listAllNormal() {
        return courseMapper.selectAllNormal();

    }

    @Override
    public List<Course> listBySubject(int i) {
        return courseMapper.listBySubject(i);

    }


}

